<?php

/** 
 * @property Water_price_mdl $price
 */
class Price extends Admin_Controller {
	
	/**
	 */
	public function __construct() {
		parent::__construct ();
		$this->load->model('Water_price_mdl','price');
		$this->load->model ( 'School_mdl', 'school' );
		$this->load->model('Admin_mdl','admin');
	}
	public function set_price(){
		
		
		$user = $_SESSION['admin'];
		$school_id=isset($user['school_id'])?$user['school_id']:0;
		
		if ( $school_id !=0 ){
			$school_id_arr = explode(',',$school_id);
		}else{
			alert('无此学校权限','back');
		}
		
		//是否为超级管理员
		$is_sup = $this->admin->isSuperManager($user['id']);
		
		//是超级管理员取出全部学校
		if ( $is_sup ){
			$school_id_arr = $this->school->getList('id');
			$school_id_arr = array_column($school_id_arr, 'id');
		}
		
		if( empty($_POST) ){

			$sid = isset($_GET['sid'])?$_GET['sid']:null;
			
			if ( !empty($sid) ){
				if ( $sid == -1 ){
					$school_id = 0;
				} elseif ( in_array($sid, $school_id_arr)){
					$school_id = $sid;
				}else{
					alert('无此学校权限','back');
				}					
			}else{
				foreach($school_id_arr as $val){
					if($val>0){
						$school_id=intval($school_id_arr[0]);break;
					}
				}
			}
			
			$school_list = $this->school->getSchoolList('',$school_id_arr);
			
			if ( $is_sup ){
				array_unshift($school_list,array('id'=>-1,'name'=>'统一默认水价'));
			}
			
			$prices['normal'] = $this->price->getPrice(BUY_MODE_NORMAL,NULL,$school_id);
// 			$prices['once']   = $this->price->getPrice(BUY_MODE_ONCE);
			$temp['school'] = $school_list;
			$temp['prices'] = $prices;
			$temp['schoolid'] = $school_id;
			
			$this->showView('admin/price/set_price',$temp);
			return ;
		}
		
		$prices = array('hot'=>array(
							'a'=>1,
							'p1'=>1,
							'p2'=>1,
						),
						'cold'=>array(
								'a'=>1,
								'p1'=>1,
								'p2'=>1,
						),
						'phot'=>array(
								'a'=>1,
						),
						'pcold'=>array(
								'a'=>1,
						),
						'once'=>300,
						'version'=>1,);
		
		$this->load->library('form_validation');
		$this->form_validation->set_rules('normalHot','正常热水','required|numeric');
		$this->form_validation->set_rules('normalCold','正常冷水','required|numeric');
		$this->form_validation->set_rules('pulseHot','脉冲热水','required|numeric');
		$this->form_validation->set_rules('pulseCold','脉冲冷水','required|numeric');
		$checkres = $this->form_validation->run();
		
		if ( !$checkres ){
			//验证失败
			$msg=validation_errors();
			error($msg);die;
		}
		
		$flag   = 0;
		foreach ( $_POST as $k=>$v ){
			$v = (float)$v;
			if ( empty($v) ) {
				continue;
			}
			switch ($k) {
				case 'normalHot':
// 					$prices[BUY_MODE_NORMAL]['hot']  = $v;
					$prices['hot']['a']  =  $v;
					$prices['hot']['p1']  = $v;
					$prices['hot']['p2']  = $v;
					$flag++;
					break;
				case 'normalCold':
// 					$prices[BUY_MODE_NORMAL]['cold'] = $v;
					$prices['cold']['a']  = $v;
					$prices['cold']['p1']  = $v;
					$prices['cold']['p2']  = $v;
					$flag++;
					break;
				case 'pulseHot':
					$prices['phot']['a']  =  $v;
					$flag++;
					break;
				case 'pulseCold':
					$prices['pcold']['a']  =  $v;
					$flag++;
					break;
// 				case 'onceHot':
// 					$prices[BUY_MODE_ONCE]['hot']  = $v;
// 					$flag++;
// 					break;
// 				case 'onceCold':
// 					$prices[BUY_MODE_ONCE]['cold'] = $v;
// 					$flag++;
// 					break;
			}
		}
		
		$school_id = (int)$_POST['school'];
		
		if ( ($school_id == -1) && ($is_sup) ){
			$school_id = 0;
		} elseif ( !in_array($school_id, $school_id_arr)){
			alert('无此学校权限','back');
		}
		
		//所有学校水价
		$dataPrice = $this->price->getPriceList();
		
		if ( $flag != 4 ) {
			alert('输入错误','back');
		}
		
		$isExist = $dataPrice[$school_id];
		
		if ( !empty($isExist) ){
			$prices['once'] = $isExist['once'];
			$prices['version'] = $isExist['version'];
		}
		
		$dataPrice[$school_id] = $prices;
		
		$res = $this->price->setPrices($dataPrice);
		
		if( $res ){
			$msg = date('Y-m-d H:i:s').' user_id : ' .$user['id'].' 修改 学校 id：'.$school_id." 水价为 ".json_encode($prices)."\n";
			$this->modify_log($msg);
			alert('修改成功','back');
		}else{
			alert('修改失败','back');
		}
		
	}
	
	public function modify_log($msg){
		$dir = APPPATH.'logs/mnglog/';
		$file = $dir.date('Y-m-d').'.php';
		file_put_contents($file,$msg,FILE_APPEND);
	}
	
	
}

